package ch13;

public class TimerInterest extends Thread {

	// Initializes BankDatabase in new variable bankDatabase
	BankDatabase bankDatabase;

	// TimerInterest constructor contains BankDatabase reference
	public TimerInterest(BankDatabase bankDatabase) {
		this.bankDatabase = bankDatabase;
	} // end constructor

	// start timer
	public void run() {
		// loop while timer is running
		while (true) {
			interest(); // execute method interest which adds interest to
						// balances
			try {
				Thread.sleep(9000); // timer runs every 9 second
			} // end try
			catch (InterruptedException e) { // catch interrupted Exception
				// This method prints a stack trace for this Throwable object
				e.printStackTrace();
			} // end catch
		} // end while loop
	} // end timer

	// adds interest to available and total balances
	public void interest() {

		// loop through bank database to get customers from customer list
		for (Customer cust : bankDatabase.getCustomerList()) {

			// loop through accounts to get all customers accounts
			for (Account ac : cust.getAccounts()) {
				ac.addInterestToCash(); // use method addInterestToCash on
										// accounts
			} // end for loop through accounts
		} // end for loop through customers
	} // end method interest
} // end class TimerInterest
